package io.renren.modules.hos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.entity.MedicineEntity;
import io.renren.modules.hos.service.MedicineService;
import io.renren.modules.hos.vo.ShopCarVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.ShopCarDao;
import io.renren.modules.hos.entity.ShopCarEntity;
import io.renren.modules.hos.service.ShopCarService;


@Service("shopCarService")
public class ShopCarServiceImpl extends ServiceImpl<ShopCarDao, ShopCarEntity> implements ShopCarService {
    @Autowired
    private MedicineService medicineService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShopCarEntity> page = this.page(
                new Query<ShopCarEntity>().getPage(params),
                new QueryWrapper<ShopCarEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ShopCarVo> queryByUser(Long userId) {
        //LambdaQueryWrapper<ShopCarEntity> queryWrapper = new LambdaQueryWrapper<>();
        //queryWrapper.eq(ShopCarEntity::getUserId, userId);
        //List<ShopCarEntity> shopCarEntities = baseMapper.selectList(queryWrapper);
        List<ShopCarEntity> shopCarEntities = baseMapper.queryByUserId(ShiroUtils.getHosUserId());

        return copyList(shopCarEntities);
    }

    @Override
    public List<ShopCarEntity> queryById(Long userId) {
        return baseMapper.queryByUserId(ShiroUtils.getHosUserId());
    }

    @Override
    public String addMedicine(Long medicineId, Integer num) {
        MedicineEntity medicineEntity = medicineService.getById(medicineId);
        ShopCarEntity shopCarEntity = new ShopCarEntity();
        shopCarEntity.setMedicineId(medicineId);
        shopCarEntity.setStatus(0);
        shopCarEntity.setCreateTime(new Date());
        shopCarEntity.setUserId(ShiroUtils.getHosUserId());
        Integer medicineNumber = medicineEntity.getMedicineNumber();
        if (medicineNumber < num){
            return "药品库存不足";
        } else {
            shopCarEntity.setMedicineNum(num);
            baseMapper.insert(shopCarEntity);

            medicineEntity.setMedicineNumber(medicineNumber - num);
            medicineService.updateById(medicineEntity);
            return "添加成功";
        }

    }

    @Override
    public Integer getPrice(Long userId) {
        List<ShopCarEntity> shopCarEntities = baseMapper.queryByUserId(ShiroUtils.getHosUserId());

        return getShopCarPrice(shopCarEntities);
    }

    public ShopCarVo copy(ShopCarEntity shopCarEntity){
        MedicineEntity medicineEntity = medicineService.getById(shopCarEntity.getMedicineId());
        ShopCarVo shopCarVo = new ShopCarVo();
        BeanUtils.copyProperties(shopCarEntity, shopCarVo);
        shopCarVo.setMedicineName(medicineEntity.getMedicineName());
        shopCarVo.setMedicineImg(medicineEntity.getMedicineImg());
        shopCarVo.setPrice(shopCarEntity.getMedicineNum() * medicineEntity.getMedicinePrice());

        return shopCarVo;
    }

    public List<ShopCarVo> copyList(List<ShopCarEntity> shopCarEntities){
        List<ShopCarVo> shopCarVos = new ArrayList<>();;
        for (ShopCarEntity shopCarEntity : shopCarEntities) {
            shopCarVos.add(copy(shopCarEntity));
        }

        return shopCarVos;
    }

    public int getMedicinePrice(ShopCarEntity shopCarEntity){
        MedicineEntity medicineEntity = medicineService.getById(shopCarEntity.getMedicineId());

        return medicineEntity.getMedicinePrice() * shopCarEntity.getMedicineNum();
    }

    public int getShopCarPrice(List<ShopCarEntity> shopCarEntities){
        int price = 0;
        for (ShopCarEntity shopCarEntity : shopCarEntities) {
            price += getMedicinePrice(shopCarEntity);
        }
        return price;
    }

}